package com.weed.helper;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import com.weed.core.WeedException;


/**
 * <p>Title: 通用维护平台1.0</p>
 * <p>Description: DES数据的加密、解密</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author Jason zhuo
 * @version 1.0
 */

public class DesHelper {

  public static byte[] getKey(String keyCode) throws Exception {
    String keyCode1 = keyCode;
    if (keyCode.length() < 8) {
      for (int i = 0; i < 8 - keyCode.length(); i++) {
        keyCode1 += "0";
      }
    }
    byte[] keyData = keyCode1.getBytes();
    return keyData;
  }

  public static byte[] EnCryptData(String keyCode, String encryptdata) throws
      Exception {
    SecureRandom sr = new SecureRandom();
    byte[] rawKeyData = getKey(keyCode);
    DESKeySpec dks = new DESKeySpec(rawKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(dks);

    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, key, sr);
    byte[] data = encryptdata.getBytes();
    byte[] encryptedData = cipher.doFinal(data);
    return encryptedData;
  }

  public static String DeCryptData(String keyCode, byte[] decryptdata) throws
      Exception {
    SecureRandom sr = new SecureRandom();
    byte[] rawKeyData = getKey(keyCode);
    DESKeySpec dks = new DESKeySpec(rawKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES", "SunJCE");
    Cipher cipher = Cipher.getInstance("DES", "SunJCE");

    /*    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        Cipher cipher = Cipher.getInstance("DES");
     */
    SecretKey key = keyFactory.generateSecret(dks);
    cipher.init(Cipher.DECRYPT_MODE, key, sr);
    byte[] encryptedData = decryptdata;
    byte[] decryptedData = cipher.doFinal(encryptedData);
    return new String(decryptedData);
  }

  public static String EnCrypt(String keyCode, String encryptdata) throws
      WeedException, Exception {
    byte[] b = null;
    b = DesHelper.EnCryptData(keyCode, encryptdata);
    String enStr = new String(Base64.encode(b), "UTF-8");
    return enStr;
  }

  public static String DeCrypt(String keyCode, String enStr) throws WeedException {
    byte[] b = null;
    String depasswd = "";

    try {
      b = Base64.decode(enStr.getBytes("UTF-8"));
      depasswd = DesHelper.DeCryptData(keyCode, b);
    }
    catch (Exception ex) {
      throw new WeedException("密码解密失败，可能是密文被篡改！");
    }
    return depasswd;
  }

  public static void main(String[] args) {
    try {
      String b = null;
      b = DesHelper.EnCrypt("Jason zhuo", "admin");
      System.out.println("Encrypt admin (Key Jason zhuo)=" + b);
      String depasswd = DesHelper.DeCrypt("Jason zhuo", "5v00WMePh3Q=");
      System.out.println("DeCrypt 5v00WMePh3Q=(Key Jason zhuo)=" + depasswd);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

}
